<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index()
    {
        $this->display();
        //echo "this is home module";
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            //$this->success('上传成功！');
            return $info;
        }
    }

    function img()
    {
        //图片处理函数
        $photoinfo = $this->upload();
        $filePath='./Public/Uploads/photo/';
        $imgfile=$photoinfo['photo']['rootpath'].'\\'.$photoinfo['savephoto']['savename'];
        //$filePath=$photoinfo['photo']['rootpath'];
        $oldFileName=$photoinfo['photo']['savename'];
        $olfFileFullName=$filePath."\\".$oldFileName;
        $newFileName='thumb_'.$oldFileName;
        $newFileFullName=$filePath."\\".$newFileName;

        $image=new\Think\Image();
        $image->open($olfFileFullName);
        $image->thumb(100,100)->save($newFileFullName);
        return $newFileName;
    }

    public function register()
    {
        //添加用户
        //用户名唯一性认证
        //密码一致验证
        //密码加密
        $userModel=D('Users');
        if($userModel->create())
        {
            if($_FILES['photo']['error']==0)//判断文件上传是否成功
            {
                //$photoinfo=$this->upload();//调用upload方法，将临时文件保存到指定位置
                $filename=$this->img();
                //var_dump($photoinfo);
                $userModel->photo=$filename;//获取文件的名字
            }
            if($userModel->add())
            {
                $this->success('注册成功',U('Admin/Index/users'));
            }
            else
            {
                $this->error('注册失败');
            }
        }
        else
        {
            $this->error($userModel->getError());
        }
    }

}